package com.corgi.test.retry;

import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

/**
 * AnnotationAwareRetryOperationsInterceptor
 *
 * @title: AnnotationAwareRetryOperationsInterceptor
 * @description:
 * @author: dengmiao
 * @create: 2020-06-10 10:05
 **/
public class AnnotationAwareRetryOperationsInterceptor implements MethodInterceptor {

    //记录重试次数
    private int times = 0;

    @Override
    public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {
        //获取拦截的方法中的Retryable注解
        Retryable retryable = method.getAnnotation(Retryable.class);
        if(retryable == null){
            return proxy.invokeSuper(obj, args);
        }
        //有Retryable注解，加入异常重试逻辑
        else {
            int maxAttempts = retryable.maxAttempts();
            try {
                return proxy.invokeSuper(obj, args);
            } catch (Throwable e) {
                if(times++ == maxAttempts){
                    System.out.println("已达最大重试次数：" + maxAttempts + ",不再重试！");
                }else{
                    System.out.println("调用" + method.getName() + "方法异常，开始第" + times +"次重试。。。");
                    //注意这里不是invokeSuper方法，invokeSuper会退出当前interceptor的处理
                    proxy.invoke(obj, args);
                }
            }
        }
        return null;
    }
}
